package com.finace.money.ui.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.support.v4.content.LocalBroadcastManager;
import android.text.SpannableString;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.finace.money.R;
import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.ACacheClearUtil;
import com.finace.money.common.util.DateUtil;
import com.finace.money.common.util.LocaleUtils;
import com.finace.money.common.util.MessageSendUtil;
import com.finace.money.common.util.PopuUtil;
import com.finace.money.common.util.StringUtil;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.AccountInfoBean;
import com.finace.money.di.component.AppComponent;
import com.finace.money.net.socket.PushReceiver;
import com.finace.money.ui.activity.AboutOurActivity;
import com.finace.money.ui.activity.AccountDetailActivity;
import com.finace.money.ui.activity.LoginActivity;
import com.finace.money.ui.activity.MainActivity;
import com.finace.money.ui.activity.MoneyInActivity;
import com.finace.money.ui.activity.MoneyOutActivity;
import com.finace.money.ui.activity.PersonInfoActivity;
import com.finace.money.ui.activity.RiskTipActivity;
import com.finace.money.ui.activity.SettleAccountActivity;
import com.finace.money.ui.widget.MyPopupWindow;

import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;

/**
 * Created by zhangpeng on 2017/11/23.
 */

public class MainMineFragment extends BaseFragment implements View.OnClickListener {

    @BindView(R.id.ll_content)
    LinearLayout llContent;
    @BindView(R.id.rl_left)
    RelativeLayout rlLeft;
    @BindView(R.id.tv_head_title)
    TextView tvHeadTitle;
    @BindView(R.id.tv_right_title)
    TextView tvRightTitle;
    @BindView(R.id.tv_mine_name)
    TextView tvMineName;
    @BindView(R.id.tv_mine_balance)
    TextView tvMineBalance;
    @BindView(R.id.rl_money_in)
    RelativeLayout rlMoneyIn;
    @BindView(R.id.rl_money_out)
    RelativeLayout rlMoneyOut;
    @BindView(R.id.tv_person_info)
    TextView tvPersonInfo;
    @BindView(R.id.tv_settle_account_list)
    TextView tvSettleAccountList;
    @BindView(R.id.tv_risk_tip)
    TextView tvRiskTip;
    @BindView(R.id.tv_about_our)
    TextView tvAboutOur;
    @BindView(R.id.tv_change_language)
    TextView tvChangeLanguage;
    @BindView(R.id.button_login_out)
    Button buttonLoginOut;
    private AccountInfoBean mAccountInfoBean;
    private Calendar mCalendar;
    private MyPopupWindow myPopupWindow;


    @Override
    public int setLayout() {
        return R.layout.fragment_mine;
    }

    @Override
    public void init() {

        rlLeft.setVisibility(View.GONE);
        tvRightTitle.setVisibility(View.VISIBLE);
        tvRightTitle.setText(R.string.account_detail);
        tvHeadTitle.setText(R.string.person_center);
        tvRightTitle.setOnClickListener(this);
        rlMoneyIn.setOnClickListener(this);
        rlMoneyOut.setOnClickListener(this);
        tvPersonInfo.setOnClickListener(this);
        tvSettleAccountList.setOnClickListener(this);
        tvRiskTip.setOnClickListener(this);
        tvAboutOur.setOnClickListener(this);
        tvChangeLanguage.setOnClickListener(this);
        buttonLoginOut.setOnClickListener(this);
        mCalendar = Calendar.getInstance();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(PushReceiver.ACTION_ACCOUNT_INFO);
        intentFilter.addAction(PushReceiver.QRY_SETTLEINFO_END);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(broadcastReceiver, intentFilter);
        unregisterReceiver(broadcastReceiver);
        mAccountInfoBean = (AccountInfoBean) ACache.get(UIUtil.getContext()).getAsObject(Constant.ACCOUNT_INFO);
        if (null != mAccountInfoBean) {
            tvMineName.setText(mAccountInfoBean.getAccount());
            String accountBalance = getString(R.string.account_balance) + mAccountInfoBean.getAvabileFunds();
            SpannableString spannableString = StringUtil.getSpannableString(accountBalance, 0, getString(R.string.account_balance).length(), R.style.text_white_14, R.style.text_YellowDDC09C_14);
            tvMineBalance.setText(spannableString);
        }
    }

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            if (null != intent) {
                String end = intent.getStringExtra(PushReceiver.QRY_SETTLEINFO_END);
                if (StringUtil.isNotNullString(end)) {
                    Intent intentSettleAccount = new Intent(UIUtil.getContext(), SettleAccountActivity.class);
                    UIUtil.startActivity(intentSettleAccount);
                }
            }
        }
    };

    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_right_title:
                Intent intentAccountDetail = new Intent(UIUtil.getContext(), AccountDetailActivity.class);
                intentAccountDetail.putExtra(PushReceiver.ACTION_ACCOUNT_INFO, mAccountInfoBean);
                UIUtil.startActivity(intentAccountDetail);
                break;
            case R.id.rl_money_in:
                Intent intentIn = new Intent(UIUtil.getContext(), MoneyInActivity.class);
                UIUtil.startActivity(intentIn);
                break;
            case R.id.rl_money_out:
                Intent intentOut = new Intent(UIUtil.getContext(), MoneyOutActivity.class);
                UIUtil.startActivity(intentOut);
                break;
            case R.id.tv_person_info:
                Intent intentPersonInfo = new Intent(UIUtil.getContext(), PersonInfoActivity.class);
                UIUtil.startActivity(intentPersonInfo);
                break;
            case R.id.tv_settle_account_list:
                //弹窗选择日期
                chooseDate();
                break;
            case R.id.tv_risk_tip:
                Intent intentRiskTip = new Intent(UIUtil.getContext(), RiskTipActivity.class);
                UIUtil.startActivity(intentRiskTip);
                break;
            case R.id.tv_about_our:
                Intent intentAboutOur = new Intent(UIUtil.getContext(), AboutOurActivity.class);
                UIUtil.startActivity(intentAboutOur);
                break;
            case R.id.tv_change_language:
                changeLanguage();
                break;
            case R.id.button_login_out:
                ACacheClearUtil.loginOut();
//                MessageSendUtil.UnregisterClient();
                UIUtil.startActivity(new Intent(UIUtil.getContext(), LoginActivity.class));
                UIUtil.showToastSafe(getString(R.string.login_out_sucess));
                break;
            case R.id.tv_chinese:
                myPopupWindow.dismiss();
                if (LocaleUtils.needUpdateLocale(UIUtil.getContext(), LocaleUtils.LOCALE_CHINESE)) {
                    LocaleUtils.updateLocale(UIUtil.getContext(), LocaleUtils.LOCALE_CHINESE);
                    restartPage();
                }
                break;
            case R.id.tv_english:
                myPopupWindow.dismiss();
                if (LocaleUtils.needUpdateLocale(UIUtil.getContext(), LocaleUtils.LOCALE_ENGLISH)) {
                    LocaleUtils.updateLocale(UIUtil.getContext(), LocaleUtils.LOCALE_ENGLISH);
                    restartPage();
                }
                break;

        }
    }

    private void restartPage() {

        getActivity().finish();
        Intent intent = new Intent(UIUtil.getContext(), MainActivity.class);
        intent.putExtra(Constant.main, 4);
        startActivity(intent);
        getActivity().overridePendingTransition(0, 0);
    }

    private void changeLanguage() {
        myPopupWindow = new PopuUtil().initAtLocationPopuWrap(getActivity(),
                R.layout.popup_language, llContent, 265, 100);
        TextView tv_chinese = myPopupWindow.getContentView().findViewById(R.id.tv_chinese);
        TextView tv_english = myPopupWindow.getContentView().findViewById(R.id.tv_english);
        tv_chinese.setOnClickListener(this);
        tv_english.setOnClickListener(this);
    }

    private void chooseDate() {
        //选中事件回调
        TimePickerView mTimePickerView = new TimePickerView.Builder(getActivity(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                String dateStr = DateUtil.DateToString(date, DateUtil.DATE_TO_STRING_SHORT_UNPATTERN);
                MessageSendUtil.QrySettleInfo(dateStr);
            }
        })
                .setTitleBgColor(Color.BLACK)
                .setCancelColor(Color.WHITE)
                .setSubmitColor(Color.WHITE)
                .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                .setLineSpacingMultiplier(12f)
                .setBgColor(Color.BLACK)
                .setCancelColor(Color.GRAY)
                .setSubmitColor(Color.GRAY)
                .setTextColorOut(Color.WHITE)
                .setTextColorCenter(Color.WHITE)
                .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setType(new boolean[]{true, true, true, false, false, false})//默认全部显示
                .setContentSize(14)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)//是否循环滚动
                .setRange(mCalendar.get(Calendar.YEAR) - 3, mCalendar.get(Calendar.YEAR))//默认是前三年
                .setLabel(getString(R.string.year), getString(R.string.month), getString(R.string.day), "", "", "")
                .build();

        mTimePickerView.show();

    }

}
